/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkSipUtil.h"

CMkSipUtil CMkSipUtil::m_Instance;
CMkSipUtil::CMkSipUtil()
    : m_TransactionCheckSec(2)
    , m_TransactionTimeOutSec(10) //send 6 times
    , m_TransactionDeleteSec(5)
    , m_bDebugMessageContent(FALSE)
{
    m_MapSipMethod["REGISTER"] = MkSipMethodRegister;
    m_MapSipMethod["INVITE"] = MkSipMethodInvite;
    m_MapSipMethod["ACK"] = MkSipMethodAck;
    m_MapSipMethod["BYE"] = MkSipMethodBye;
    m_MapSipMethod["MESSAGE"] = MkSipMethodMessage;
    m_MapSipMethod["INFO"] = MkSipMethodInfo;
    m_MapSipMethod["SUBSCRIBE"] = MkSipMethodSubscribe;
    m_MapSipMethod["NOTIFY"] = MkSipMethodNotify;

    m_MapStatusCodeDesc[MkSipStatusTrying] = "Trying";
    m_MapStatusCodeDesc[MkSipStatusRinging] = "Ringing";
    m_MapStatusCodeDesc[MkSipStatusCallForwarded] = "Call Is Being Forwarded";
    m_MapStatusCodeDesc[MkSipStatusQueued] = "Queued";
    m_MapStatusCodeDesc[MkSipStatusSessionProgress] = "Session Progress";
    m_MapStatusCodeDesc[MkSipStatusOK] = "OK";
    m_MapStatusCodeDesc[MkSipStatusMultipleChoices] = "Multiple Choices";
    m_MapStatusCodeDesc[MkSipStatusMovedPermanently] = "Moved Permanently";
    m_MapStatusCodeDesc[MkSipStatusMovedTemporarily] = "Moved Temporarily";
    m_MapStatusCodeDesc[MkSipStatusUseProxy] = "Use Proxy";
    m_MapStatusCodeDesc[MkSipStatusAlternativeService] = "Alternative Service";
    m_MapStatusCodeDesc[MkSipStatusBadRequest] = "Bad Request";
    m_MapStatusCodeDesc[MkSipStatusUnauthorized] = "Unauthorized";
    m_MapStatusCodeDesc[MkSipStatusPaymentRequired] = "Payment Required";
    m_MapStatusCodeDesc[MkSipStatusForbidden] = "Forbidden";
    m_MapStatusCodeDesc[MkSipStatusNotFound] = "Not Found";
    m_MapStatusCodeDesc[MkSipStatusMethodNotAllowed] = "Method Not Allowed";
    m_MapStatusCodeDesc[MkSipStatusNotAcceptable] = "Not Acceptable";
    m_MapStatusCodeDesc[MkSipStatusProxyAuthenticationRequired] = "Proxy Authentication Required";
    m_MapStatusCodeDesc[MkSipStatusRequestTimeout] = "Request Timeout";
    m_MapStatusCodeDesc[MkSipStatusGone] = "Gone";
    m_MapStatusCodeDesc[MkSipStatusRequestEntityTooLarge] = "Request Entity Too Large";
    m_MapStatusCodeDesc[MkSipStatusRequestURITooLong] = "Request-URI Too Long";
    m_MapStatusCodeDesc[MkSipStatusUnsupportedCodec] = "Unsupported Media Type";
    m_MapStatusCodeDesc[MkSipStatusUnsupportedURIScheme] = "Unsupported URI Scheme";
    m_MapStatusCodeDesc[MkSipStatusBadExtension] = "Bad Extension";
    m_MapStatusCodeDesc[MkSipStatusExtensionRequired] = "Extension Required";
    m_MapStatusCodeDesc[MkSipStatusIntervalTooBrief] = "Interval Too Brief";
    m_MapStatusCodeDesc[MkSipStatusTemporarilyUnavailable] = "Temporarily Unavailable";
    m_MapStatusCodeDesc[MkSipStatusCallDoesNotExist] = "Call/Transaction Does Not Exist";
    m_MapStatusCodeDesc[MkSipStatusLoopDetected] = "Loop Detected";
    m_MapStatusCodeDesc[MkSipStatusTooManyHops] = "Too Many Hops";
    m_MapStatusCodeDesc[MkSipStatusAddressIncomplete] = "Address Incomplete";
    m_MapStatusCodeDesc[MkSipStatusAmbiguous] = "Ambiguous";
    m_MapStatusCodeDesc[MkSipStatusBusyHere] = "Busy Here";
    m_MapStatusCodeDesc[MkSipStatusRequestTerminated] = "Request Terminated";
    m_MapStatusCodeDesc[MkSipStatusNotAcceptableHere] = "Not Acceptable Here";
    m_MapStatusCodeDesc[MkSipStatusRequestPending] = "Request Pending";
    m_MapStatusCodeDesc[MkSipStatusUndecipherable] = "Undecipherable";
    m_MapStatusCodeDesc[MkSipStatusServerInternalError] = "Server Internal Error";
    m_MapStatusCodeDesc[MkSipStatusNotImplemented] = "Not Implemented";
    m_MapStatusCodeDesc[MkSipStatusBadGateway] = "Bad Gateway";
    m_MapStatusCodeDesc[MkSipStatusServiceUnavailable] = "Service Unavailable";
    m_MapStatusCodeDesc[MkSipStatusServerTimeOut] = "Server Time-out";
    m_MapStatusCodeDesc[MkSipStatusVersionNotSupported] = "Version Not Supported";
    m_MapStatusCodeDesc[MkSipStatusMessageTooLarge] = "Message Too Large";
    m_MapStatusCodeDesc[MkSipStatusBusyEverywhere] = "Busy Everywhere";
    m_MapStatusCodeDesc[MkSipStatusDecline] = "Decline";
    m_MapStatusCodeDesc[MkSipStatusDoesNotExistAnywhere] = "Does Not Exist Anywhere";
    m_MapStatusCodeDesc[MkSipStatusNotAcceptable606] = "Not Acceptable";
}

Uint32 CMkSipUtil::GetSipMethod(const MkString& strMethod, MkSipMethod& SipCmd) const
{
    MkUnorderedMap<MkString, MkSipMethod>::const_iterator it = m_MapSipMethod.find(strMethod);
    if (it == m_MapSipMethod.end()) {
        return InvalidParams;
    }
    SipCmd = it->second;
    return NoneError;
}

Uint32 CMkSipUtil::GetSipStringMethod(const MkSipMethod& SipMethod, MkString& StrMethod) const
{
    MkUnorderedMap<MkString, MkSipMethod>::const_iterator it = m_MapSipMethod.begin();
    for (; it != m_MapSipMethod.end(); it++) {
        if (it->second == SipMethod) {
            StrMethod = it->first;
            return NoneError;
        }
    }
    return InvalidParams;
}

Uint32 CMkSipUtil::GetStatusCodeDesc(const MkSipStatus& Status, MkString& CodeDesc) const
{
    MkUnorderedMap<Uint32, MkString>::const_iterator it = m_MapStatusCodeDesc.find(Status);
    if (it != m_MapStatusCodeDesc.end()) {
        CodeDesc = it->second;
        return NoneError;
    }
    return InvalidParams;
}

CMkSipUtil::~CMkSipUtil()
{

}